'use strict';
define([
    'angular',
    'ngRoute',
    'services/getApiServices',
], function(angular) {
    return angular.module('myApp.tasks', [
            'ngRoute',
            'myApp.services.getApiServices',
        ])
        .config(['$routeProvider', function($routeProvider) {
            $routeProvider
                .when('/tasks/:page', {
                    templateUrl: '../template/tasks.html',
                    controller: 'tasksCtrl'
                })
        }])
        .controller('tasksCtrl', [
            '$scope',
            '$route',
            '$location',
            '$routeParams',
            'ApiIterface',
            function($scope, $route, $location, $routeParams, ApiIterface) {
                $scope.userinfo = {};
                $scope.querystr = {}
                switch ($routeParams.page) {
                    case 'yes':
                        $scope.querystr.complete = true;
                        break;
                    case 'no':
                        $scope.querystr.complete = false;
                        break;
                    default:
                        break;
                };
                ApiIterface
                    .getListByParams($scope.querystr)
                    .then(rev => {
                        console.log(rev);
                        $scope.userinfo = rev.data.userinfo
                        $scope.tasks = rev.data.data;
                    })
                    .catch(err => console.log(err));

                $scope.tasks = [];
                $scope.taskInput = '';

                $scope.addTask = () => {
                    ApiIterface
                        .AddTask($scope.taskInput)
                        .then(rev => {
                            $scope.tasks = rev.data.data
                                .filter(d => d.complete == $scope.querystr.complete);
                        })
                        .catch(err => console.error(err))
                    $scope.taskInput = '';
                };
                $scope.DelTask = (task) => {
                    ApiIterface
                        .DelTask(task)
                        .then(rev => {
                            $scope.tasks = rev.data.data
                                .filter(d => d.complete == $scope.querystr.complete);
                        })
                        .catch(err => console.error(err))
                }
                $scope.UpdateTask = (task) => {
                    if (task.task == "" || task.task == undefined) {
                        // console.log('task不能为空！');
                        alert('任务不能为空！');
                        return false;
                    }
                    ApiIterface
                        .UpdateTask(task)
                        .then(rev => {
                            console.log(rev.data);
                            $scope.CacelEdit();
                        })
                        .catch(err => console.error(err))
                };
                $scope.Edit = {
                    isEditing: false,
                    CurrentId: '',
                };
                $scope.EditTask = (task) => {
                    $scope.Edit.isEditing = true;
                    $scope.Edit.CurrentId = task._id;
                };
                $scope.CacelEdit = () => {
                    $scope.Edit = {
                        isEditing: false,
                        CurrentId: '',
                    };
                }
                $scope.logout = () => {
                    ApiIterface
                        .logout()
                        .then(rev => {
                            let data = rev.data;
                            alert(data.msg);
                            $location.path('/login');
                            console.log(rev.data);
                        })
                        .catch(err => console.error(err))
                }
            }
        ]);
});